<!--
 * @Descripttion: HN-SaaS管理系统
 * @version: 1.0.0
 * @Author: 侯建军
 * @Date: 2020-09-08 16:19:58
 * @LastEditors: 侯建军
 * @LastEditTime: 2020-09-09 11:43:55
-->
<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" :before-close="handleClose">
      <el-form class="patientInfo"  :rules="rules" ref="patientform" :model="patient" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="patient.name"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="cardId">
          <el-input v-model="patient.cardId"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="patient.gender">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="birth">
          <el-date-picker
            type="date"
            placeholder="出生年月"
            v-model="patient.birth"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            style="width: 50%;"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="地址" prop="cardId">
          <el-input type="textarea" v-model="patient.address"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { insert, update } from "@/api/patient.js";

export default {
  name: "",
  props: ["dialogVisible"],
  data() {
    return {
      patient: {gender:"男"},
      rules: {
        name: [
          { required: true, message: "患者姓名不可为空！", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        cardId: [
          { required: true, message: "身份证号不可为空！", trigger: "blur" },
          {
            min: 15,
            max: 18,
            message: "长度必须为15~18个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handleClose() {
      this.$emit("close", this.dialogVisible);
    },
    save() {
      if(this.patient.gender=="男"){
        this.patient.gender=0;
      }else{
          this.patient.gender=1;
      }
      // 添加记录
      insert(this.patient).then((res) => {
        if (res.data.code === "0000") {
          this.$message({
            type: "success",
            message: res.data.msg,
          });
          this.$emit("close", this.dialogVisible);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.patientInfo {
  text-align: left;
}
</style>